//
//  Auction_SpecialSaleList_VC_WX.m
//  ZITHER
//
//  Created by weixin on 2019/6/1.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "Auction_SpecialSaleList_VC_WX.h"
#import "Auction_Main_CollectionView_WX.h"
#import "specialSaleSceneid.h"


@interface Auction_SpecialSaleList_VC_WX ()

@property (nonatomic,strong)Auction_Main_CollectionView_WX * CollectionView;

@end

@implementation Auction_SpecialSaleList_VC_WX

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HexColor(0xeeeeee);
    [self setUI];
    [self loadData];
}

-(void)setNavTtitle:(NSString *)NavTtitle{
    _NavTtitle = NavTtitle;
    BaseLab * titleLab = [BaseLab initCustomLab:NavTtitle color:HexColor(0x333333) font:[UIFont fontWithName:@"PingFang-SC-Regular" size:17]];
    [self.baseNavigationView addSubview:titleLab];
    
    [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseNavigationView).offset(30);
        make.bottom.equalTo(self.baseNavigationView).offset(-10);
    }];
    
    BaseLab * titleSubLab = [BaseLab initCustomLab:@"下一场5月24日10:00开卖" color:HexColor(0xff6f6f) font:[UIFont fontWithName:@"PingFang-SC-Regular" size:15]];
    [self.baseNavigationView addSubview:titleSubLab];
    
    [titleSubLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.baseNavigationView).offset(-15);
        make.bottom.equalTo(self.baseNavigationView).offset(-5);
    }];
}



-(void)loadData{
//    NSString *str = @"cfd7442d3e854ac6ae826b894cba768c";
    NSInteger page = self.CollectionView.page;
    [[serviceDefualt loadspecialSaleSceneid:self.id page:page reponseObjctClass:[specialSaleSceneid class]]subscribeNext:^(ResponseData * x) {
        if (x.success) {
            specialSaleSceneid *modelRes = x.responseData;
            if (modelRes.list.count == 0) {
                if (page == 0 ) {
                    [Tools showTips:@"暂无更多数据"];
                    [self.CollectionView.mj_header endRefreshing];
                    [self.CollectionView.mj_footer endRefreshing];
                }else{
                    [self.CollectionView.mj_footer endRefreshingWithNoMoreData];
                }
                return ;
            }
            
            if (page == 0) {
                [self changeData:x.responseData];
            }else{
                specialSaleSceneid *model = x.responseData;
                for (specialSaleSceneidList *modelList in model.list) {
                    [self.CollectionView.model.list addObject:modelList];
                }
                model.list = self.CollectionView.model.list;
                self.CollectionView.model = model;
                [self.CollectionView reloadData];
            }
            [self.CollectionView.mj_header endRefreshing];
            [self.CollectionView.mj_footer endRefreshing];
            
        }else{
            [Tools showTips:x.errorInfo];
        }
        
    }];
    
}

-(void)changeData:(specialSaleSceneid*)model{
    
    NSMutableArray <specialSaleSceneidList *>*arrM = [[NSMutableArray alloc]init];
    
    for (int jjj =0; jjj<2; jjj++) {
        specialSaleSceneidList *modelList = model.list[jjj];
        modelList.recommend = @"ture";
    }
    
    
    
    int i =0;
    for (specialSaleSceneidList *modelList in model.list) {
        if ([modelList.recommend isEqualToString: @"ture"]) {
            [arrM addObject:modelList];
            i++;
        }
    }
    model.recommendCount = i;

    for (specialSaleSceneidList *modelList in model.list) {
        if (![modelList.recommend isEqualToString: @"ture"]) {
            [arrM addObject:modelList];
        }
    }
    model.list = arrM;

    self.CollectionView.model = model;
    [self.CollectionView reloadData];
}



-(void)setUI{
    [self.view addSubview:self.CollectionView];
    [self.CollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(NaviHeight);
        make.left.right.equalTo(self.view).offset(0);
        make.bottom.equalTo(self.view).offset(-kBottomSafeHeight);
    }];
}

-(Auction_Main_CollectionView_WX *)CollectionView{
    if (!_CollectionView) {
        _CollectionView  = [[Auction_Main_CollectionView_WX alloc]initWithFrame:CGRectZero collectionViewLayout:[Auction_Main_CollectionView_WX GetLayyout]];
        @weakify(self);
        _CollectionView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            @strongify(self);
            self.CollectionView.page ++;
            [self loadData];
        }];
        _CollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            @strongify(self);
            self.CollectionView.page = 0;
            [self loadData];
        }];
    }
    return _CollectionView;
}


#pragma mark - Life Cycle

- (void)dealloc {
    self.CollectionView.delegate = nil;
    self.CollectionView.dataSource = nil;
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self updateLayoutForOrientation:[UIApplication sharedApplication].statusBarOrientation];
}

-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
    [self updateLayoutForOrientation:toInterfaceOrientation];
}

- (void)updateLayoutForOrientation:(UIInterfaceOrientation)orientation {
    CHTCollectionViewWaterfallLayout *layout =
    (CHTCollectionViewWaterfallLayout *)self.CollectionView.collectionViewLayout;
    layout.columnCount = UIInterfaceOrientationIsPortrait(orientation) ? 2 : 3;
}



@end
